package com.lyf.utils;

import cn.hutool.json.JSONObject;
import com.lyf.pojo.Document;
import com.lyf.result.Code;
import com.lyf.result.Result;
import lombok.Data;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import redis.clients.jedis.Jedis;

import javax.print.Doc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Upload {


    public static void uploadFile(HttpServletRequest req, HttpServletResponse resp){
        // 创建磁盘项目工厂
        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();

        // 创建解析类
        ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);

        try {
            // 获取请求中的属性
            List<FileItem> list = servletFileUpload.parseRequest(req);

            // 循环遍历属性中的值
            for (FileItem fileItem : list) {
                // 判断是否为文件属性
                if (fileItem.isFormField()) {
                    String name = fileItem.getFieldName();
                    String value = fileItem.getString();

                    System.out.println(name + "-" + value);
                } else {
                    // 获取文件的文件名
                    String fileName = fileItem.getName();
//                    int dotIndex = fileName.lastIndexOf('.');
//                    String extension = dotIndex != -1 ? fileName.substring(dotIndex) : "";
//                    System.out.println("extension:"+extension);
//                    //避免文件名重复导致覆盖,加上时间戳
//                    long time = new Date().getTime();
//                    String timeString = Long.toString(time);
//                    //重新命名
//                    fileName = timeString+extension;
                    // 获取文件大小
                    long fileSize = fileItem.getSize();

                    System.out.println("文件名为" + fileName + ",文件大小为" + fileSize+",文件类型"+fileItem.getContentType());

                    // 获取文件的输入流
                    InputStream inputStream = fileItem.getInputStream();

                    // 创建存放文件的路径
                    String path = "";
//                    if(fileItem.getContentType().equals("image/jpeg") || fileItem.getContentType().equals("image/png")){
//                        path = "D:\\粤嵌Java\\第二阶段\\项目\\前端\\hrm\\src\\assets\\img\\";
//                    }else{
                        path = "D:\\粤嵌Java\\第二阶段\\项目\\前端\\hrm\\src\\files\\";
//                    }
                    System.out.println(path);
                    // 创建输出流
                    OutputStream outputStream = new FileOutputStream(path + fileName);
                    int len = 0;
                    byte[] bytes = new byte[1024];
                    while ((len = inputStream.read(bytes)) != -1) {
                        // 输出流写入文件
                        outputStream.write(bytes, 0, len);
                    }

                    // 关闭输入输出流
                    inputStream.close();
                    outputStream.close();


                    JSONObject jsonObject = new JSONObject();
                    Result result = new Result(Code.OK,fileName,"文件上传成功");
                    Map<String,Object> map = new HashMap<>();
                    map.put("result",result);
                    map.put("fileType",fileItem.getContentType());
                    jsonObject.putAll(map);
                    resp.getWriter().println(jsonObject.toString());

                    //存储到redis
                    Jedis jedis = new Jedis("127.0.0.1",6379);
                    jedis.auth("123456");
                    //过期时间为2分钟
                    jedis.setex("filename",60*2,fileName);
                    jedis.setex("filetype",60*2,fileItem.getContentType());
                }
            }
        } catch (FileUploadException | IOException e) {
            e.printStackTrace();
        }
    }
}
